using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Logger;

namespace FiltronASP.Models.DAO
{
    public class ModuleTemplateDAO : Abstracts.AbstractGenericDAO<ModuleTemplate, System.Nullable<int>>,
        Interfaces.IModuleTemplateDAO
    {
        /// <summary>
        /// Metoda zwracajaca nazwe parametru bedacego kluczem glownym tabeli.
        /// </summary>
        /// <returns>nazwa klucza glownego</returns>
        protected override string GetKeyProperty()
        {
            return "module_template_id";
        }

        /// <summary>
        /// Pobieranie listy obiektów na podstawie języka.
        /// </summary>
        /// <param name="languageId">język</param>
        /// <returns>Lista rekordów o zadanym języku</returns>
        public IQueryable<ModuleTemplate> FindByLanguage(int languageId)
        {
            if (languageId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<ModuleTemplate> moduleTemplates = this._cmsEntitiesDataContext.ModuleTemplates.Where(
                    mety => mety.template_id.HasValue && (this._cmsEntitiesDataContext.Templates.Where(
                        temp => temp.language_id.HasValue && temp.language_id.Value.Equals(languageId)
                        ).Select(te => te.template_id).Contains(mety.template_id.Value)
                        )
                        );
                if (!(moduleTemplates.Any()))
                {
                    return null;
                }   //if
                return moduleTemplates;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Wstawianie listy obiektów na podstawie jezyka.
        /// </summary>
        /// <param name="defaultLanguageId">domyslny jezyk</param>
        /// <param name="newLanguageId">nowy jezyk</param>
        /// <returns>true jesli sie udalo, false wpp</returns>
        public bool InsertForNewLanguage(int defaultLanguageId, int newLanguageId)
        {
            if (defaultLanguageId.Equals(0) || newLanguageId.Equals(0))
            {
                return false;
            }   //if
            try
            {

                IQueryable<ModuleTemplate> moduleTemplates = this.FindByLanguage(defaultLanguageId);
                ModuleDAO _moduleDAO = new ModuleDAO();
                TemplateDAO _templateDAO = new TemplateDAO();
                if (moduleTemplates == null)
                {
                    return true;
                }   //if
                if (!(moduleTemplates.Any()))
                {
                    return true;
                }   //if
                foreach (ModuleTemplate moTe in moduleTemplates)
                {
                    ModuleTemplate moduleTemplateToInsert = new ModuleTemplate();
                    moduleTemplateToInsert.module_template_created = DateTime.Now;
                    moduleTemplateToInsert.module_template_modified = DateTime.Now;
                    Module oldModule = _moduleDAO.FindByPK(moTe.module_id);
                    if (oldModule == null)
                    {
                        return false;
                    }
                    Module newModule = _moduleDAO.FindByLanguageIdAndModuleName(newLanguageId, oldModule.module_name);
                    if (newModule == null)
                    {
                        return false;
                    }
                    moduleTemplateToInsert.module_id = newModule.module_id;
                    Template oldTemplate = _templateDAO.FindByPK(moTe.template_id);
                    if (oldTemplate == null)
                    {
                        return false;
                    }
                    Template newTemplate = _templateDAO.FindByLangAndName(newLanguageId, oldTemplate.template_name);
                    if (newTemplate == null)
                    {
                        return false;
                    }
                    moduleTemplateToInsert.template_id = newTemplate.template_id;
                    this.Add(moduleTemplateToInsert);
                }

            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return true;
        }

        /// <summary>
        /// Usuwanie obiektow po jezyku.
        /// </summary>
        /// <param name="languageId">jezyk</param>
        /// <returns>true dla sukcesu, false w przeciwnym razie</returns>
        public bool DeleteByLanguage(int languageId)
        {
            if (languageId.Equals(0))
            {
                return false;
            }   //if
            try
            {
                IQueryable<ModuleTemplate> moduleTemplates = this.FindByLanguage(languageId);
                if (moduleTemplates == null)
                {
                    return true;
                }   //if
                if (!(moduleTemplates.Any()))
                {
                    return true;
                }   //if
                foreach (ModuleTemplate modeTe in moduleTemplates)
                {
                    this._cmsEntitiesDataContext.ModuleTemplates.DeleteOnSubmit(modeTe);
                }
                try
                {
                    _cmsEntitiesDataContext.SubmitChanges();

                }
                catch (Exception ex)
                {
                    MyLogger.Log(ex);
                }   //catch
                return true;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return false;
        }
    }
}
